//设置周下拉框
export function setweekOption(year) { //传入年份,字符串类型年份,'2020'
	year = new Date().getFullYear()
	let days = getDay(year || new Date().getFullYear())
	let weeks = {};
	for (let i = 0; i < days.length; i++) {
		let weeksKeyLen = Object.keys(weeks).length;
		let daySplit = days[i].split('_');

		if (weeks[weeksKeyLen] === undefined) {
			weeks[weeksKeyLen + 1] = [days[i]]
		} else {
			if (daySplit[1] == '1') {
				weeks[weeksKeyLen + 1] = [days[i]]
			} else {
				weeks[weeksKeyLen].push(days[i])
			}
		}
	}
	let option = []
	let weeksKeyLen = Object.keys(weeks).length;
	for (let i = 1; i < weeksKeyLen + 1; i++) {
		let obj = {};
		obj.year = year
		obj.week = i
		// obj.md = year + '.' + weeks[i][0].replace(/\//g, ".") + '-' + year + '.' + weeks[i][weeks[i].length - 1].replace(/\//g, ".")
		obj.md = weeks[i][0].replace(/\//g, ".").slice(0,5) + '-' + weeks[i][weeks[i].length - 1].replace(/\//g, ".").slice(0,5)
		obj.s = weeks[i][0]
		obj.e = weeks[i][weeks[i].length - 1]

		obj.text = "第" + i + "周" + '(' + weeks[i][0].slice(0,5) + '-' + weeks[i][weeks[i].length - 1].slice(0,5) + ')';
		obj.value = i - 1;
		obj.detialis = weeks[i]
		option.push(obj)

	}
	return option;
}

export function getToday() {
	var today = new Date()
	var year = new Date().getFullYear() //本年
	var month = today.getMonth() + 1; //本月
	var day = today.getDate() //本日
	let data = formatNumber(month) + '/' + formatNumber(day)
	return data;
}
export function getDay(year) {
	let dates = [];
	for (let i = 1; i <= 12; i++) {
		for (let j = 1; j <= new Date(year, i, 0).getDate(); j++) {
			dates.push(formatNumber(i) + '/' + formatNumber(j) + '_' + new Date([year, formatNumber(i), formatNumber(j)]
				.join('-')).getDay()) //返回当年所有日期（带星期数）
		}
	}
	return dates;
}
export function formatNumber(n) {
	return n.toString().length > 1 ? n : '0' + n
}


export function curWeek() {
	const today = new Date();
	let firstDayOfYear = new Date(today.getFullYear(), 0, 1);
	const dayOfWeek = firstDayOfYear.getDay();
	let spendDay = 1;
	if (dayOfWeek != 0) {
		spendDay = 7 - dayOfWeek + 1;
	}
	firstDayOfYear = new Date(today.getFullYear(), 0, spendDay);
	const d = Math.ceil((today.valueOf() - firstDayOfYear.valueOf()) / 86400000);
	return Math.ceil(d / 7);
}
